package com.cby.third.cases.pdf

import android.content.Context
import android.net.http.SslError
import android.util.AttributeSet
import android.webkit.*
import com.cby.code.util.CbyLogUtils
import kotlin.jvm.JvmOverloads

/**
 * @author ChenBaoyang
 * @description: PDFView
 * @date 2021/12/1 14:45
 */
class PdfView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : WebView(context, attrs, defStyleAttr) {

    init {
        val settings: WebSettings = this.settings
        settings.javaScriptEnabled = true
        settings.allowFileAccess = true
        settings.allowFileAccessFromFileURLs = true
        settings.allowUniversalAccessFromFileURLs = true

        settings.builtInZoomControls = true
        settings.setSupportZoom(true)
        settings.displayZoomControls = true //不显示缩放按钮

        this.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                CbyLogUtils.flog("加载完成")
            }

            override fun onReceivedError(
                view: WebView, request: WebResourceRequest,
                error: WebResourceError?
            ) {
                CbyLogUtils.flog("onReceivedError：" + (error?.toString() ?: "网页加载失败"))
            }

            override fun onReceivedSslError(
                view: WebView, handler: SslErrorHandler,
                error: SslError
            ) {
                handler.proceed()
            }
        }
    }

    fun loadOnlinePdfWithToolBox(url: String) {
        this.loadUrl("http://mozilla.github.io/pdf.js/web/viewer.html?file=${url}")
    }

    fun loadOnlinePdf(url: String) {
        this.loadUrl("file:///android_asset/PDFjs/PdfTemplate.html?${url}")
    }

    /**
     * 加载assert目录下的pdf文件
     * @param path String
     */
    fun loadAssertPdfFileWithToolBox(path: String){
        //this.loadUrl("file:///android_asset/PDFjs/web/viewer.html?file=../sample.pdf")
        this.loadUrl("file:///android_asset/PDFjs/web/viewer.html?file=../${path}")
    }

    /**
     * 加载assert目录下的pdf文件
     * @param path String
     */
    fun loadAssertPdfFile(path: String){
        //this.loadUrl("file:///android_asset/PDFjs/web/viewer.html?file=../sample.pdf")
        this.loadUrl("file:///android_asset/PDFjs/PdfTemplate.html?../${path}")
    }




}